import storage from "@/utils/storage";
import constant from "@/utils/constant";

const user = {
  state: {
    token: uni.getStorageSync("token"),
    name: storage.get(constant.name),
    avatar: storage.get(constant.avatar),
    flag: storage.get(constant.flag),
    userId: storage.get(constant.userId),
    roles: storage.get(constant.roles),
    permissions: storage.get(constant.permissions),
    deviceId: "",
    merchantInfo: storage.get(constant.merchantInfo),
    wx_merchantInfo: storage.get(constant.wx_merchantInfo),
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, name) => {
      state.name = name;
      storage.set(constant.name, name);
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
      storage.set(constant.avatar, avatar);
    },
    SET_FLAG: (state, flag) => {
      state.flag = flag;
      storage.set(constant.flag, flag);
    },
    SET_USERID: (state, userId) => {
      state.userId = userId;
      storage.set(constant.userId, userId);
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
      storage.set(constant.roles, roles);
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions;
      storage.set(constant.permissions, permissions);
    },
    SET_DEVICE_ID(state, value) {
      state.deviceId = value; // 定义一个mutation来修改状态
    },
    SET_MINFO: (state, merchantInfo) => {
      state.merchantInfo = merchantInfo;
      storage.set(constant.merchantInfo, merchantInfo);
    },
    SET_WX_MINFO: (state, wx_merchantInfo) => {
      state.wx_merchantInfo = wx_merchantInfo;
      storage.set(constant.wx_merchantInfo, wx_merchantInfo);
    },
    SET_WX_MINFO_LOGO: (state, merchantLogo) => {
      state.wx_merchantInfo.merchantLogo = merchantLogo;
      storage.set(constant.wx_merchantInfo, state.wx_merchantInfo.merchantLogo);
    },
  },

  actions: {},
};

export default user;
